package com.xmindguoguo.boot.modular.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xmindguoguo.boot.common.constant.GlobalConstant;
import com.xmindguoguo.boot.config.properties.ConfigurationKit;
import com.xmindguoguo.boot.modular.system.model.TSystemMenuModel;
import com.xmindguoguo.boot.modular.system.service.IAutoGenerateMenuService;
import com.xmindguoguo.boot.modular.system.service.ITSystemMenuService;
import com.xmindguoguo.boot.util.CmUtil;
import com.xmindguoguo.boot.util.spring.SpringMvcUtil;

@Service
@Transactional
public class AutoGenerateMenuServiceImpl implements IAutoGenerateMenuService {
    @Autowired
    ITSystemMenuService systemMenuService;

    public void generMenuList(WebApplicationContext wac) {
        Set<String> urlSet = SpringMvcUtil.getHandlerHandlerMappingAllUrl(wac);
        Set<String> databaseUrlSet = this.getDatabaseUrlSet();
        urlSet.removeAll(databaseUrlSet); // 移除掉与数据库重复的url
        // 进行插入
        Long autoMenuId = ConfigurationKit.CONFIG.getLong("autoMenuId", 1L); // 自动生成主键id
        // 先判断是否存在这个其他类型的数据 ，存在就不插入，否则插入
        TSystemMenuModel other = systemMenuService.selectById(autoMenuId);
        if (other == null) {
            TSystemMenuModel newMenuModel = new TSystemMenuModel();
            newMenuModel.setId(autoMenuId);
            newMenuModel.setPId(0L);
            newMenuModel.setPIds(autoMenuId.toString());
            newMenuModel.setName("其他");
            newMenuModel.setStatus(GlobalConstant.MODEL_ADD);
            newMenuModel.setCreateTime(new Date());
            newMenuModel.setCreateUserId(GlobalConstant.Const.ADMIN_ID);
            newMenuModel.setTips("默认系统自动生成权限菜单的父类");
            newMenuModel.setMenuType(GlobalConstant.MenuType.BUTTION); // 暂时为按钮
            newMenuModel.setType(GlobalConstant.MenuControlType.NO_LONGIN);
            systemMenuService.insert(newMenuModel);
        }
        this.insertMenuList(urlSet, autoMenuId);
    }

    public Set<String> getDatabaseUrlSet() {
        // 查询数据库库的所有的url
        Wrapper<TSystemMenuModel> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("reqUrl"); // 只查询请求url
        wrapper.isNotNull("reqUrl");
        wrapper.ne("status", GlobalConstant.MODEL_DEL);
        List<TSystemMenuModel> menuList = systemMenuService.selectList(wrapper);
        // 比较那些url需要插入数据库
        Set<String> databaseUrlSet = new HashSet<>();// 存储所有url集合
        if (CollectionUtils.isNotEmpty(menuList)) {
            for (TSystemMenuModel menuModel : menuList) {
                if (StringUtils.isNotEmpty(menuModel.getReqUrl())) {
                    databaseUrlSet.add(menuModel.getReqUrl());
                }
            }
        }
        return databaseUrlSet;
    }

    public void insertMenuList(Set<String> urlSet, Long autoMenuId) {
        List<TSystemMenuModel> newMenuList = new ArrayList<>();
        TSystemMenuModel newMenuModel = null;
        for (String reqUrl : urlSet) {
            newMenuModel = getMenuModel(autoMenuId, reqUrl);
            newMenuList.add(newMenuModel);
        }
        if (!newMenuList.isEmpty()) {
            systemMenuService.insertBatch(newMenuList);
        }
    }

    private TSystemMenuModel getMenuModel(Long autoMenuId, String reqUrl) {
        TSystemMenuModel newMenuModel = new TSystemMenuModel();
        newMenuModel.setId(CmUtil.getPkId());
        newMenuModel.setPId(autoMenuId);
        newMenuModel.setPIds(autoMenuId + "_" + newMenuModel.getId());
        newMenuModel.setName(reqUrl);
        newMenuModel.setReqUrl(reqUrl);
        newMenuModel.setStatus(GlobalConstant.MODEL_ADD);
        newMenuModel.setCreateTime(new Date());
        newMenuModel.setCreateUserId(GlobalConstant.Const.ADMIN_ID);
        newMenuModel.setTips("系统自动生成，请勿删除");
        newMenuModel.setMenuType(GlobalConstant.MenuType.BUTTION); // 暂时为按钮
        newMenuModel.setType(GlobalConstant.MenuControlType.NO_LONGIN);
        return newMenuModel;
    }
}
